package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.CarService;

/**
 * Servlet implementation class CarDeal
 */
@WebServlet("/files/CarDeal")
public class CarDeal extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String ERROR_PAGE = "CarFailed.jsp";
	private static final String SUCCESS_PAGE = "list_CLCX.jsp";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CarDeal() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");
		String flag=request.getParameter("flag");
		
		switch(flag){
			case "add":{
				if( !CarService.carAdd(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "back":{
				if( !CarService.carBack(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "delete":{
				if( !CarService.carDelete(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "edit":{
				if( !CarService.carEdit(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "export":{
				String excel=CarService.carExport(request);
				if( excel!=null ) {
					response.sendRedirect("../"+excel);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "import":{
				if( !CarService.carImport(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "out":{
				if( !CarService.carOut(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			default:break;
		}
	}

}
